package com.example.myapplication.activity.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private FrameLayout mBodtLayout;
    private LinearLayout mBottomlayout;
    private View mCourseBtn, mExercisesBtn, mMyinfoBtn;
    private TextView tv_course, tv_exercises, tv_myinfo;
    private ImageView iv_course, iv_exercises, iv_myinfo;
    private TextView tv_back, tv_main_title;
    private RelativeLayout r1_title_bar;
    private MyInfoView mMyInfoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
        initBottomBar();
        setListener();
        setInitStatus();
    }

    private void init() {
        tv_back = findViewById(R.id.tv_back);
        tv_main_title = findViewById(R.id.tv_main_title);
        r1_title_bar = findViewById(R.id.title_bar);
        tv_main_title.setText("博学谷课程");
        r1_title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        tv_back.setVisibility(View.GONE);
        initBodtLayout();
    }

    private void initBottomBar() {
        mBottomlayout = findViewById(R.id.main_button_bar);
        mCourseBtn = findViewById(R.id.bottom_bar_course_btn);
        mExercisesBtn = findViewById(R.id.bottom_bar_exercises_btn);
        mMyinfoBtn = findViewById(R.id.bottom_bar_myinfo_btn);
        tv_course = findViewById(R.id.bottom_bar_text_course);
        iv_course = findViewById(R.id.bottom_bar_image_course);
        tv_exercises = findViewById(R.id.bottom_bar_text_exercises);
        iv_exercises = findViewById(R.id.bottom_bar_image_exercises);
        tv_myinfo = findViewById(R.id.bottom_bar_text_myinfo);
        iv_myinfo = findViewById(R.id.bottom_bar_image_myinfo);
    }

    private void initBodtLayout() {
        mBodtLayout = findViewById(R.id.main_body);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bottom_bar_course_btn:
                clearBottomImageState();
                selectDisplayView(0);
                break;
            case R.id.bottom_bar_exercises_btn:
                clearBottomImageState();
                selectDisplayView(1);
                break;
            case R.id.bottom_bar_myinfo_btn:
                clearBottomImageState();
                selectDisplayView(2);
                break;
            default:
                break;
        }
    }

    private void setListener() {
        for (int i = 0; i < mBottomlayout.getChildCount(); i++) {
            mBottomlayout.getChildAt(i).setOnClickListener(this);
        }
    }

    private void clearBottomImageState() {
        tv_course.setTextColor(Color.parseColor("#666666"));
        tv_exercises.setTextColor(Color.parseColor("#666666"));
        tv_myinfo.setTextColor(Color.parseColor("#666666"));
        iv_course.setImageResource(R.drawable.main_course_icon);
        iv_exercises.setImageResource(R.drawable.main_exercises_icon);
        iv_course.setImageResource(R.drawable.main_my_icon);
        for (int i = 0; i < mBottomlayout.getChildCount(); i++) {
            mBottomlayout.getChildAt(i).setSelected(false);
        }
    }

    private void setSelectedStatus(int index) {
        switch (index) {
            case 0:
                mCourseBtn.setSelected(true);
                iv_course.setImageResource(R.drawable.main_course_icon_selected);
                tv_course.setTextColor(Color.parseColor("#0097F7"));
                r1_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("博学谷课程");
                break;
            case 1:
                mExercisesBtn.setSelected(true);
                iv_exercises.setImageResource(R.drawable.main_exercises_icon_selected);
                tv_exercises.setTextColor(Color.parseColor("#0097F7"));
                r1_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("博学谷习题");
                break;
            case 2:
                mMyinfoBtn.setSelected(true);
                iv_myinfo.setImageResource(R.drawable.main_my_icon_selected);
                tv_myinfo.setTextColor(Color.parseColor("#0097F7"));
                r1_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("我的");
                break;
        }
    }

    private void removeAllView() {
        for (int i = 0; i < mBodtLayout.getChildCount(); i++) {
            mBodtLayout.getChildAt(i).setVisibility(View.GONE);
        }
    }

    private void setInitStatus() {
        clearBottomImageState();
        setSelectedStatus(0);
        createView(0);
    }

    private void selectDisplayView(int index) {
        removeAllView();
        createView(index);
        setSelectedStatus(index);
    }

    private void createView(int viewindex) {
        switch (viewindex) {
            case 0:
                Toast.makeText(this, "课程界面创建", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(this, "习题界面创建", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                if (mMyInfoView == null){
                    mMyInfoView = new MyInfoView(this);
//                   mBodtLayout.addView(mMyInfoView.getView())
                }else {
                    mMyInfoView.getView();
                }
                mMyInfoView.showView();
                break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(data != null){
            boolean isLogin = data.getBooleanExtra("isLogin",false);
            if(isLogin){
                clearBottomImageState();
                selectDisplayView(0);
            }
            if (mMyInfoView != null){
                mMyInfoView.setLoginParms(isLogin);
            }
        }
    }

    protected long exitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - exitTime > 20000) {
                Toast.makeText(this, "在按一次退出博学谷", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                MainActivity.this.finish();
                if (readLoginStatus()) {
                    clearLoginStatus();
                }
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private boolean readLoginStatus() {
        SharedPreferences sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isLogin = sp.getBoolean("isLogin", false);
        return isLogin;
    }

    private void clearLoginStatus() {
        SharedPreferences sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isLogin", false);
        editor.putString("loginUserName", "");
        editor.commit();
    }

}